package st.mark.quadtris.states;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import mdes.slick.sui.Button;
import mdes.slick.sui.Container;
import mdes.slick.sui.Display;
import mdes.slick.sui.TextField;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import st.mark.highscores.HighscoreBoard;
import st.mark.highscores.HighscoreItem;
import st.mark.quadtris.BackgroundShapes;
import st.mark.quadtris.GameInfoData;
import st.mark.quadtris.MusicManager;
import st.mark.quadtris.Quadtris;

public class HighScores extends BasicGameState {

	private int STATE_ID = -1;
	
	private Image template = null;
	
	private BackgroundShapes bg;
	
	private HighscoreBoard hs_Quad = null;
	private HighscoreBoard hs_Norm = null;
	
	ArrayList<HighscoreItem> scores_Quad = null;
	ArrayList<HighscoreItem> scores_Norm = null;

	private AngelCodeFont font = null;
	
	Display display = null;
	TextField txtName = null;
	Button btnSubmit = null;
	
	public HighScores(int state){
		this.STATE_ID = state;
	}
	
	public int getID() {
		return STATE_ID;
	}

	public void enter(GameContainer gc, StateBasedGame sbg){
		GameInfoData.SHOW_SHAPES = true;
		
		scores_Quad = getQuadScores();
		scores_Norm = getNormalScores();
		
		GameInfoData.SHOW_LOADING = false;
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		font = new AngelCodeFont("st/mark/resources/highscores/hi-scores.fnt", "st/mark/resources/highscores/hi-scores.png");
		
		//SUI
		display = new Display(gc);
		//g.drawRect(100, 475, 600, 50);
		Container container = new Container();
		container.setSize(600, 50);
		container.setLocation(100, 475);
		
		txtName = new TextField(10);
		txtName.setMaxChars(10);
		txtName.setLocation(25, txtName.getHeight()/2+3);
		txtName.setForeground(new Color(Color.black));
		txtName.updateAppearance();
		container.add(txtName);
		
		btnSubmit = new Button("Submit Score");
		btnSubmit.setLocation(txtName.getX()+txtName.getWidth()+25,btnSubmit.getHeight()/2);
		btnSubmit.setForeground(new Color(Color.black));
		btnSubmit.pack();
		container.add(btnSubmit);
		
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				add_score();
			}
		});
		
		display.add(container);
		
		
		hs_Quad = new HighscoreBoard("f1b5a91d4d6ad523f2610114591c007e75d15084");
		hs_Norm = new HighscoreBoard("84a4d59947e66b60fca0a3f356658a3476058c66");
		
		template = new Image("st/mark/resources/highscores/highscore_bg.png");
		
		bg = new BackgroundShapes();
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		bg.render();
		
		template.draw(0, 0);
		
		int y = 140;
		for(int i = 0; i < scores_Quad.size(); i++){
			HighscoreItem hsi = scores_Quad.get(i);
			font.drawString(115, y, hsi.getRank() + " - " + hsi.getScore() + " " + hsi.getText1());
			y += 25;
		}
		
		
		y = 140;
		for(int i = 0; i < scores_Norm.size(); i++){
			HighscoreItem hsi = scores_Norm.get(i);
			font.drawString(485, y, hsi.getRank() + " - " + hsi.getScore() + " " + hsi.getText1());
			y += 25;
		}
		
		if(GameInfoData.HISCORESHOW){
			display.render(gc, g);
			font.drawString(400, 482, "Submit Score: "+GameInfoData.HISCORE);
		} else if(GameInfoData.HSI != null){
			String mode = "Quadtris";
			if(GameInfoData.HSI.getText2().equalsIgnoreCase("1")) mode = "normal";
			font.drawString(110, 482, "Rank: "+GameInfoData.HSI.getRank()+"  Score: "+GameInfoData.HSI.getScore()+" Mode: "+mode, new Color(Color.green));
		}
			
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		MusicManager.update();
		display.update(gc, delta);
		bg.update(delta);
		
		Input input = gc.getInput();
		int mx = input.getMouseX(), my = input.getMouseY();
		
		if(mx >= 224 && mx <= 602 && my >= 561 && my < 585 && input.isMousePressed(Input.MOUSE_LEFT_BUTTON))
			sbg.enterState(Quadtris.MENU_STATE);
	}

	private ArrayList<HighscoreItem> getQuadScores(){
		return hs_Quad.getScorePage(12, 1);
	}
	
	private ArrayList<HighscoreItem> getNormalScores(){
		return hs_Norm.getScorePage(12, 1);
	}
	
	private void add_score(){
		
		try {
			String name = URLEncoder.encode(txtName.getText(), "UTF-8");
			
			if(GameInfoData.HIGAMEMODE == GameInfoData.MODE_QUAD)
				GameInfoData.HSI = hs_Quad.addNewScore(GameInfoData.HISCORE, name, "0", "0", "0", "0");
			else if (GameInfoData.HIGAMEMODE == GameInfoData.MODE_NORM)
				GameInfoData.HSI = hs_Norm.addNewScore(GameInfoData.HISCORE, name, "1", "0", "0", "0");
		
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		};
		GameInfoData.SHOW_LOADING = true;
		scores_Quad = getQuadScores();
		scores_Norm = getNormalScores();
		GameInfoData.SHOW_LOADING = false;
		
		GameInfoData.HISCORESHOW = false;
		
	}
}
